﻿
// Left 4 Dead 2 CheaterDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "Left 4 Dead 2 Cheater.h"
#include "Left 4 Dead 2 CheaterDlg.h"
#include "afxdialogex.h"

#include "tlhelp32.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define log(fmt, ...) \
CString str; \
str.Format(CString(fmt), __VA_ARGS__); \
AfxMessageBox(str);

static CLeft4Dead2CheaterDlg *mainDlg;
static HANDLE monitorThread;
static HANDLE gameProcess;
static DWORD processId;
static DWORD pbase;

static DWORD WINAPI ThreadFunc(LPVOID p)
{
	while (true)
	{
		HWND gameWindow = FindWindow(CString("Valve001"), CString("Left 4 Dead 2"));//获取游戏窗口
		//HWND gameWindow = FindWindow(CString("TXGuiFoundation"), CString("QQ"));//获取游戏窗口
		if (gameWindow == NULL) {
			mainDlg->m_check_zd.EnableWindow(FALSE);
			mainDlg->m_check_hp.EnableWindow(FALSE);
			mainDlg->m_check_zd.SetCheck(FALSE);
			mainDlg->m_check_hp.SetCheck(FALSE);

			gameProcess = NULL;
			processId = 0;

			pbase = 0;
		} else if (gameProcess == NULL) {
			mainDlg->m_check_zd.EnableWindow(FALSE);
			mainDlg->m_check_hp.EnableWindow(FALSE);
			mainDlg->m_check_zd.SetCheck(FALSE);
			mainDlg->m_check_hp.SetCheck(FALSE);

			GetWindowThreadProcessId(gameWindow, &processId);//获取游戏进程ID
			gameProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);//打开游戏进程

			pbase = 0;
		} else if (pbase == 0) {
			//遍历模块
			HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId);
			if (!hSnap) {
				log("%d", hSnap);
				return false;
			}
			MODULEENTRY32 pe;
			pe.dwSize = sizeof(pe);
			BOOL MoudleAdd = Module32First(hSnap, &pe);
			while (MoudleAdd) {
				if (pe.szModule == CString("server.dll")) {
					pbase = (DWORD)pe.modBaseAddr;
					log("%s", pe.szModule);
					mainDlg->m_check_zd.EnableWindow(TRUE);
					mainDlg->m_check_hp.EnableWindow(TRUE);
					break;
				}
				MoudleAdd = Module32Next(hSnap, &pe);
			}
		}
		Sleep(1000);
	}
	return NULL;
}

//void WriteMemory(void *value, DWORD valueSize, ...) {
//	if (value == NULL || valueSize == NULL || gameProcess == NULL) return;
//
//	DWORD tempValue = 0;
//
//	va_list addresses;
//	va_start(addresses, valueSize);
//	DWORD offset = 0;
//	DWORD lastAddress = 0;
//	while ((offset = va_arg(addresses, DWORD)) != -1)
//	{
//		lastAddress = tempValue + offset;
//		::ReadProcessMemory(gameProcess, (LPCVOID)lastAddress, &tempValue, valueSize, NULL);
//	}
//	va_end(addresses);
//
//	BOOL r = ::WriteProcessMemory(gameProcess, (LPVOID) lastAddress, value, valueSize, NULL);
//	log("%d",r);
//}
//void WriteMemory(void* value, DWORD valueSize, DWORD address) {
//	WriteMemory(value, valueSize, address, -1);
//}

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CLeft4Dead2CheaterDlg 对话框



CLeft4Dead2CheaterDlg::CLeft4Dead2CheaterDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LEFT4DEAD2CHEATER_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLeft4Dead2CheaterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CHECK_zd, m_check_zd);
	DDX_Control(pDX, IDC_CHECK_hp, m_check_hp);
}

BEGIN_MESSAGE_MAP(CLeft4Dead2CheaterDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_zd, &CLeft4Dead2CheaterDlg::OnBnClickedCheckzd)
	ON_BN_CLICKED(IDC_CHECK_hp, &CLeft4Dead2CheaterDlg::OnBnClickedCheckhp)
	ON_BN_CLICKED(IDC_BUTTON_jc, &CLeft4Dead2CheaterDlg::OnBnClickedButtonjc)
END_MESSAGE_MAP()


// CLeft4Dead2CheaterDlg 消息处理程序

BOOL CLeft4Dead2CheaterDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	mainDlg = this;
	monitorThread = CreateThread(NULL, 0, ThreadFunc, 0, 0, NULL);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CLeft4Dead2CheaterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CLeft4Dead2CheaterDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CLeft4Dead2CheaterDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CLeft4Dead2CheaterDlg::OnBnClickedCheckzd()
{
	//MessageBox(str, CString("错误"), MB_YESNO | MB_ICONERROR);
	//TRACE("错误");
	//AfxMessageBox(str);
	//MessageBoxA(0,0,0,0);
	//bool checked = IsDlgButtonChecked(IDC_CHECK_zd);
	//CButton *button = (CButton *) GetDlgItem(IDC_CHECK_zd);

	if (m_check_zd.GetCheck()) {
		LPVOID addr = (LPVOID)(pbase + 0x3D2AE0);
		BYTE data[] = { 0x90,0x90,0x90,0x90,0x90,0x90 };
		::WriteProcessMemory(gameProcess, addr, data, sizeof(data), NULL);
		DWORD err = GetLastError();
		log("%d", err);
	}
	else {
		LPVOID addr = (LPVOID)(pbase + 0x3D2AE0);
		BYTE data[] = { 0x89,0xBE,0x14,0x14,0x00,0x00 };
		::WriteProcessMemory(gameProcess, addr, data, sizeof(data), NULL);
		DWORD err = GetLastError();
		log("%d", err);
	}
}


void CLeft4Dead2CheaterDlg::OnBnClickedCheckhp()
{
	if (m_check_hp.GetCheck()) {
		LPVOID addr = (LPVOID)(pbase + 0x3F2A1);
		BYTE data[] = { 0x90, 0x90 };
		::WriteProcessMemory(gameProcess, addr, data, sizeof(data), NULL);
		DWORD err =  GetLastError();
		log("%d", err);
	}
	else {
		LPVOID addr = (LPVOID)(pbase + 0x3F2A1);
		BYTE data[] = { 0x89, 0x37 };
		::WriteProcessMemory(gameProcess, addr, data, sizeof(data), NULL);
		DWORD err = GetLastError();
		log("%d", err);
	}
}


void CLeft4Dead2CheaterDlg::OnBnClickedButtonjc()
{
	ShellExecute(NULL, CString("open"), CString("http://www.xiexueyuan.com"), NULL, NULL, SW_SHOWNORMAL);
}
